# coding=utf-8
from extractors.base_extractor import BaseExtractor
import pymongo
from scrapy.selector import Selector
from lxml import etree
from spidertools.utils.snippets import combine_two_dict


class MongoDb(object):

    def __init__(self):
        self.my_cli = pymongo.MongoClient("mongodb://127.0.0.1:27017")
        self.my_db = self.my_cli["construction"]
        self.my_col = self.my_db["project_html"]
        self.select_url = []

    @property
    def query_condition(self):
        return {"source_type": "金蝉电子招投标综合交易平台"}

    def get_db_info(self):
        for info in self.my_col.find(self.query_condition):
            yield info


# 金蝉电子招投标综合交易平台
class JinChanDianZiZhaoTouBiaoZongHeJiaoYiPingTai(BaseExtractor):

    def __init__(self, info_dict):
        super(JinChanDianZiZhaoTouBiaoZongHeJiaoYiPingTai, self).__init__(info_dict)
        self.html = etree.XML(info_dict['html']).xpath("//contentText/text()")[0]
        self.sel = Selector(text=self.html)

    def commom_announcement_parse(self):
        '''
        通用网页公告解析
        :return:
        '''

        text_parsed_dicts = self.common_text_parse()
        print(text_parsed_dicts)
        table_parsed_dicts = self.common_table_parse()
        output = {}
        if text_parsed_dicts:
            output = combine_two_dict(output, text_parsed_dicts)
        if table_parsed_dicts:
            output = combine_two_dict(output, table_parsed_dicts)

        return output


if __name__ == '__main__':
    m = MongoDb()
    for info in m.get_db_info():
        print(info["origin_url"])
        j = JinChanDianZiZhaoTouBiaoZongHeJiaoYiPingTai(info)
        j.commom_announcement_parse()
        break